
$(document).ready(function () {
    initPage();
    loadData();
});

//初始化页面元素
function initPage() {
    addEvent();
}

//加载数据
function loadData() {
    [tblName]List();
}

//添加事件
function addEvent() {

    //添加按钮事件
    $("#btn_[tblName]_add").click(function(){
        Init[tblName]AddForm();
		$('#div_[tblName]_window_add').window('open');
   });

    //修改按钮事件
    $("#btn_[tblName]_edit").click(function () {
        var selectRow = $("#table_[tblName]_list").datagrid("getSelected");
        if (selectRow == null) {
            $.messager.alert("操作提示", "请选择要编辑的数据行！");
        } else {
            Init[tblName]EditForm(selectRow);
            $('#div_[tblName]_window_edit').window('open');
        }
    });

    // 删除按钮事件
    $("#btn_[tblName]_delete").click(function () {
        var selectRow = $("#table_[tblName]_list").datagrid("getSelected");
        if (selectRow == null) {
            $.messager.alert("操作提示", "请选择要删除的数据行！");
            return;
        }
        [tblName]Delete(selectRow);
    });

    //刷新按钮事件
    $("#btn_[tblName]_reload").click(function () {
        $('#table_[tblName]_list').datagrid('reload');
    });

    //查询按钮事件
    $("#btn_[tblName]_search").click(function () {
        [tblName]List();
    });

    //添加表单提交
    $("#btn_[tblName]_add_submit").click(function () {
        var formId = "form_[tblName]_add";
        var popupWindowId = "div_[tblName]_window_add";
        if (!$("#" + formId).form('validate')) return;
        var formData = getFormData(formId);
        // formData["id"]="id";
        doSubmit(formData, popupWindowId);
    });

    //修改表单提交
    $("#btn_[tblName]_edit_submit").click(function () {
        var formId = "form_[tblName]_edit";
        var popupWindowId = "div_[tblName]_window_edit";
        if (!$("#" + formId).form('validate')) return;
        var formData = getFormData(formId);
        // formData["id"]="id";
        doSubmit(formData, popupWindowId);
    });


    //修改表单提交
    $("#btn_view_close").click(function () {
        $('#div_[tblName]_window_view').window('close');
    });}

//获取datagrid列表数据
function [tblName]List() {

    var filter = {};
    $("#toolbar input").each(function () {
		filter[$(this).attr("name")] = $(this).val();
    });
    //filter["id"] = $("#txtQryId").textbox("getValue");
    var tblId = "table_[tblName]_list";
    var columns = [
        [
[loop start]
               { field: '[fldName]', title: '[fldTitle]', width: 80 },
[loop end]^ 
        ]
    ];
    $("#" + tblId).datagrid({
        dataType: 'json',
        url: "/api/[tblName]/list",
        queryParams: {  filter: JSON.stringify(filter) },
        columns: columns,
        loadMsg: '数据加载中,请稍后...',
        onLoadSuccess: function (data) {
            console.log("success");
            console.log(data);
            $("#closed-windows").removeClass("none");
        },
        onLoadError: function (data) {
            $.messager.alert('错误提示', '后端程序出错', 'error');
            console.log("error");
            console.log(data);
        },
        onClickRow: function (rowIndex, rowData) {
        },
        onDblClickRow: function (rowIndex, rowData) {
            Init[tblName]EditForm(rowData);
            $('#div_[tblName]_window_edit').window('open');
        },
        width: 'auto',
        height: 'auto',
        nowrap: false,
        striped: true,
        collapsible: false,
        remoteSort: false,
        rownumbers: true,
        fitColumns: true,
        singleSelect: true,
        pageNumber: 1,
        pagination: true,
        pageList: [50, 100],
        pageSize: 50
    });
    //设置分页控件
    var p = $("#" + tblId).datagrid('getPager');
    $(p).pagination({
        beforePageText: '第',
        afterPageText: '页 共 {pages} 页',
        displayMsg: '当前显示 {from} - {to} 条记录   共 {total} 条记录'

    });
}

//添加表单赋初始值
function Init[tblName]AddForm() {
    $('#form_[tblName]_add').form('clear');
    var row = new Object();
    //row.id = 0;
    $('#form_[tblName]_add').form('load', row);
}

//把model数据赋值到表单
function Init[tblName]EditForm(selectRow) {
    var id = selectRow.id;
    $.ajax({
        type: "get",
        url: "/api/[tblName]/GetById/?id=" + id,
        //data: unionGuid,
        success: function (result) {
            if (result.statecode == 0) {
                $('#form_[tblName]_edit').form('load', result.data);
            } else {
                console.log(result);
            }
            //if (msg.canEdit == 0) $("input[name='RoleName']").attr("disabled", true);//$("#uRoleName").
            //else $("input[name='RoleName']").attr("disabled", false);
        }
    });


}

//删除
function [tblName]Delete(selectRow) {
    $.messager.confirm("操作提示", "您确定要删除本条信息吗，删除后不可恢复？", function (YorN) {
        if (YorN) {
            $.ajax({
                url: "/api/[tblName]/delete/?id=" + selectRow.id,
                type: "get",
                data: {  },
                success: function (result) {
                    if (result.statecode == 0) {
                        [tblName]List();
                    }
                    else {
                        $.messager.alert("提示", "删除失败", "warning");
                    }
                }
            });
        }
    });
}

//保存
function doSubmit(formData, popupWindowId) {
    //console.log(formData);
    //var formData = getFormData(popupWindowId, method);
    $.ajax({
        type: "POST",
        url: "/api/[tblName]/edit/",
        data: { "formData": JSON.stringify(formData) },
        success: function (data) {
            console.log("提交返回结果：")
            console.log(data);
            if (data.statecode==0) {
                $.messager.alert('提示', "提交成功！", 'info');
                $('#' + popupWindowId).window('close');
                $('#table_[tblName]_list').datagrid('reload');
            } else {
                $.messager.alert('提示', "提交失败！", 'info');
            }
        }, error: function (ex) {
            $.messager.alert('提示', "后台错误！", 'warn');
        }
    });
}


function f_getUrl(value, row, index) {
    return String.Format("<a href='javascript:;' onclick='[tblName]View(\"{0}\",\"{1}\")'>{2}</a>", row, index, value);
}
function [tblName]View(id, index) {
    $("#table_[tblName]_list").datagrid("selectRow", index);

    var selectRow = $("#table_[tblName]_list").datagrid("getSelected");

    $.ajax({
        type: "get",
        url: "/api/[tblName]/GetById/?id=" + selectRow.id,
        success: function (result) {
            if (result.statecode == 0) {
                console.log("view begin:");
                //console.log(result);
                //$('#form_[tblName]_view easyui-span').form('load', result.data);
                for (var k in result.data) {
                    var v = result.data[k];
                    console.log(k+","+v);
                    $('.easyui-span[id="spn-' + k + '"]').text(v);
                }
            } else {
                console.log(result);
            }

        }
    });

    $('#div_[tblName]_window_view').window('open');

}

